package com.miplus.ccrm.core.service.crd;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.base.MPJBaseServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.miplus.ccrm.common.CmdResVo;
import com.miplus.ccrm.common.JPubUtils;
import com.miplus.ccrm.common.JSysUser;
import com.miplus.ccrm.common.JUsrPerm;
import com.miplus.ccrm.core.controller.vo.credit.CrdCardReqVo;
import com.miplus.ccrm.core.controller.vo.credit.CrdCardResVo;
import com.miplus.ccrm.core.controller.vo.credit.CrdCaseResVo;
import com.miplus.ccrm.core.pojo.*;
import com.miplus.ccrm.core.mapper.CreditCardMapper;
import com.miplus.ccrm.core.security.JCommonData;
import org.springframework.stereotype.Service;
import java.util.List;

/**
* @author pc
* @description 针对表【credit_card(信用卡信息表)】的数据库操作Service实现
* @createDate 2024-04-01 10:45:47
*/
@Service
public class CreditCardServiceImpl extends MPJBaseServiceImpl<CreditCardMapper, CreditCard>
    implements CreditCardService{

    @Override
    public IPage<CrdCardResVo> crdCardList(JSysUser user,CrdCardReqVo paras)
    {
        String cardstar= JUsrPerm.getPermission(user,JUsrPerm.DISSTAR_CARD);
        String $usrid=user.getUsrid();
        return selectJoinListPage(new Page<>(paras.getPageNum(), paras.getPageSize()),
                CrdCardResVo.class,
                new MPJLambdaWrapper<CreditCard>()
                        .selectAs("fun_getnames('BUSINESS.TYPE',cda.acct_big_type)", CrdCardResVo::getProdtype)
                        .selectAs("fun_getnames('CREDIT.ACCT.TYPE',cda.acct_type)", CrdCardResVo::getAcctType)
                        .selectAs("fun_getnames('CREDIT.CARD.TYPE',t.card_type)", CrdCardResVo::getCardType)
                        .selectAs("fun_getnames('CREDIT.CARD.STATUS',t.card_status)", CrdCardResVo::getCardStatus)
                        .selectAs("fun_getnames('CREDIT.CARD.MAINAUX',t.hold_card_seq_num)", CrdCardResVo::getHoldCardSeqNum)
                        .selectAs(String.format("fi_starSecu('%s','%s',t.card_no,'%s')",$usrid,JUsrPerm.DISSTAR_CARD,cardstar),CrdCardResVo::getCardNo)
                        .leftJoin(ConCustomer.class,"cu",ConCustomer::getCustomernumber,CreditCard::getCustNo)
                        .leftJoin(CreditAccount.class,"cda",CreditAccount::getAcct,CreditCard::getAcct)
                        .select(CreditCard::getCardId,CreditCard::getCancCardDt,CreditCard::getActivationDt,
                                CreditCard::getCardFirstNum,CreditCard::getIssueDt,CreditCard::getPromoteOperName,CreditCard::getPromoteOperNo,
                                CreditCard::getPromoteOperTeamName,CreditCard::getPromoteOperTelNo,CreditCard::getMddt,
                                CreditCard::getAcct)
                        .select(ConCustomer::getCustname,ConCustomer::getCustenname)
                        .eq(CreditCard::getCustNo,paras.getCustid())
        );
    }

    @Override
    public CmdResVo getAccOpenLastCardNo(JSysUser user,String acctno)
    {
        String cardno=getAccOpenLastCardNo(acctno);
        if(!JUsrPerm.hasPermission(user,JUsrPerm.DISSTAR_CARD)){
            cardno=JPubUtils.getStarString(JPubUtils.STAR_CARD,cardno);
        }

        return new CmdResVo(1,cardno);
    }

    @Override
    public String getAccOpenLastCardNo(String acctno)
    {
        String cardno="";
        List<CreditCard> ll=selectJoinList(CreditCard.class,
                new MPJLambdaWrapper<CreditCard>()
                        .eq(CreditCard::getAcct,acctno)
                        .selectAll(CreditCard.class)
                        .orderByDesc(CreditCard::getActivationDt)
        );
        if(JPubUtils.IsNotEmpty(ll)){
            cardno = ll.get(0).getCardNo();
        }
        return cardno;
    }

    @Override
    public CmdResVo getCustOpenLastCardNo(JSysUser user, String custno)
    {
        int ret=0;
        String cardno="";
        List<CreditCard> ll=selectJoinList(CreditCard.class,
                new MPJLambdaWrapper<CreditCard>()
                        .eq(CreditCard::getCustNo,custno)
                        .selectAll(CreditCard.class)
                        .orderByDesc(CreditCard::getActivationDt)
        );
        if(JPubUtils.IsNotEmpty(ll)){
            ret=1;
            cardno=ll.get(0).getCardNo();
            if(!JUsrPerm.hasPermission(user,JUsrPerm.DISSTAR_CARD)){
                cardno=JPubUtils.getStarString(JPubUtils.STAR_CARD,cardno);
            }
        }

        return new CmdResVo(ret,cardno);
    }
}




